﻿#include "componentbase.h"
#include "ComponentManager.h"
#include "Component_log.h"

ComponentBase::ComponentBase(QObject* parent)
	: QObject(parent)
{
	m_ob = component;
	m_name.clear();
	m_type = Component::component_null;
	connect(this, &ComponentBase::PublishWarningEvent, m_ob, &ComponentManager::DealWithComponentWarningEvent);
	connect(this, &ComponentBase::PublishSystemEvent, m_ob, &ComponentManager::DealWithComponentSystemEvent);
}

ComponentBase::~ComponentBase()
{}

void ComponentBase::SetErrmsg(const QString& str, bool send)
{
	m_errmsg = str;
	log_error("{}", m_errmsg);
	if (send) {
		emit PublishSystemEvent((int)m_type, (int)ComponentEvent::system_event_stop);
	}
}

void ComponentBase::SetWarningmsg(const QString& str)
{
	QString m_errmsg = m_name + u8"告警事件:" + str;
	log_error("{}", m_errmsg);
	emit PublishWarningEvent((int)m_type, m_errmsg);
}

QString ComponentBase::GetComponentName()
{
	return m_name;
}
QString ComponentBase::GetComponentErrMsg()
{
	return m_errmsg;
}

Component ComponentBase::GetComponentType()
{
	return m_type;
}

void ComponentBase::SetVersion(const QString& version)
{
	m_version = version;
}
QString ComponentBase::GetVersion()
{
	return m_version;
}
/***************************************************
* 初始化接口, 组件加载时调用
* 返回值判断， 0：表示成功  -1表示失败，程序可以继续， -99表示严重失败，程序停止
*************************************/
int ComponentBase::Init()
{
	int ret = ComponentInit();
	if (ret == 0) {		// 组件初始化成功后，对组件信息进行校验
		if (m_name.isEmpty()) {
			SetWarningmsg(u8"组件初始化异常：组件名称为空");
		}
		if (m_type <= Component::component_null || m_type >= Component::component_max) {
			SetWarningmsg(u8"组件初始化异常：组件类型错误");
		}
	}

	return ret;
}

/***************************************************
* 组件复位接口, 清空组件错误
*
*************************************/
bool ComponentBase::Reset()
{
	m_errmsg.clear();
	return ComponentSelfReset();
}

/****************************************************************************
 * 功能: 组件状态检查
 * 传参:
 * 返回值:
 ****************************************************************************/
bool ComponentBase::CheckState()
{
	return ComponentCheckSelfState();
}
